package com.dsg.workflow;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dsg.entity.apply.DsgApplyBill;
import com.dsg.entity.apply.DsgApplyCar;
import com.dsg.entity.apply.DsgApplyCard;
import com.dsg.entity.approve.DsgApproveCase;
import com.dsg.service.apply.*;
import com.dsg.service.system.IDsgSysFileService;
import com.dsg.util.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 轉換油咭（現有客戶->新客戶）
 */
@Slf4j
@Service
public class WorkflowService16 extends AbstractWorkflowService {
    @Autowired
    IDsgApplyCarService carService;
    @Autowired
    IDsgApplyBillService billService;
    @Autowired
    IDsgApplyPayService payService;
    @Autowired
    IDsgApplyCusService cusService;
    @Autowired
    IDsgSysFileService fileService;
    @Autowired
    WorkflowEngine engine;
    @Autowired
    IDsgApplyCardService cardService;

    @Override
    protected String recalculateNodeName(DsgApplyBill bill) {
        if (Constant.PAY_TYPE_ATP.equals(bill.getPayType())) {
            return NODE_INPUT_CAR_INFO;
        } else {
            return NODE_DEDUCT_ONE;
        }
    }

    @Override
    protected boolean canGoOn(DsgApplyBill bill) throws Exception {
        boolean pass = billService.internalAudit(bill);
        return pass;
    }

    @Override
    protected Set getConditionalNodeSet(DsgApplyBill bill) {
        Set<String> conditionalNodeSet = new HashSet<>();
        if (Constant.PAY_TYPE_ATP.equals(bill.getPayType())) {
            conditionalNodeSet.add(NODE_DDA);
            conditionalNodeSet.add(NODE_CHECK_LIST);
        } else {
            conditionalNodeSet.add(NODE_CHECK_LIST);
        }
        return conditionalNodeSet;
    }

    @Override
    public void checkListFillBack(DsgApproveCase approveCase) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("APP_NO", approveCase.getAppNo());
        DsgApplyBill bill = getBillByAppNo(approveCase.getAppNo());
        if (Constant.PAY_TYPE_CDC.equals(bill.getPayType())) {
            tranValidate(approveCase.getAppNo());
        }
    }

    @Override
    public void deductOneFillBack(DsgApproveCase approveCase) {
        DsgApplyBill bill = getBillByAppNo(approveCase.getAppNo());
        if (Constant.PAY_TYPE_ATP.equals(bill.getPayType())) {
            tranValidate(approveCase.getAppNo());
        }
    }

//    /**
//     * 轉移油咭正式生效，變更數據狀態
//     */
//    @Transactional(rollbackFor = Exception.class)
//    void tranValidate(String appNo) {
//        log.info("appNo:{}, 轉移油咭正式生效，變更數據狀態", appNo);
//        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.select("CAR_NO", "OID");
//        wrapper.eq("APP_NO", appNo);
//        List<DsgApplyCar> carList = carService.list(wrapper);
//        carList.forEach(e -> e.setCarStatus(DsgApplyCar.CAR_STATUS_EFFECT));
//        carService.updateBatchById(carList);
//
//        List<String> carNoList = carList.stream().map(e -> e.getCarNo()).collect(Collectors.toList());
//        LambdaUpdateWrapper<DsgApplyCar> updateWrapper = new LambdaUpdateWrapper<>();
//        updateWrapper.in(DsgApplyCar::getCarNo, carNoList);
//        updateWrapper.ne(DsgApplyCar::getAppNo, appNo);
//        DsgApplyCar car = new DsgApplyCar();
//        car.setCarStatus(DsgApplyCar.CAR_STATUS_TRAN);
//        carService.update(car, updateWrapper);
//
//        wrapper = new QueryWrapper();
//        wrapper.select("CARD_NO", "OID");
//        wrapper.eq("APP_NO", appNo);
//        List<DsgApplyCard> cardList = cardService.list(wrapper);
//        cardList.forEach(e -> e.setCardStatus(DsgApplyCard.CARD_STATUS_4));
//        cardService.updateBatchById(cardList);
//
//        List<String> cardNoList = cardList.stream().map(e -> e.getCardNo()).collect(Collectors.toList());
//        LambdaUpdateWrapper<DsgApplyCard> updateWrapper1 = new LambdaUpdateWrapper<>();
//        updateWrapper1.in(DsgApplyCard::getCardNo, cardNoList);
//        updateWrapper1.ne(DsgApplyCard::getAppNo, appNo);
//        DsgApplyCard card = new DsgApplyCard();
//        card.setCardStatus(DsgApplyCard.CARD_STATUS_9);
//        cardService.update(card, updateWrapper1);
//    }
}
